//
//  ViewController.m
//  20508
//
//  Created by student on 16/3/24.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "Operation.h"
@interface ViewController ()
@property(nonatomic,strong) NSOperationQueue *queue;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
//    queue.maxConcurrentOperationCount=1;
//    NSInvocationOperation *op1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download1) object:nil];
//    NSInvocationOperation *op2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(download2) object:nil];
//    NSBlockOperation *op3=[NSBlockOperation blockOperationWithBlock:^{
//        NSLog(@"%@:%s start",[NSThread currentThread],__func__);
//        [NSThread sleepForTimeInterval:0.1];
//        NSLog(@"%@:%s end",[NSThread currentThread],__func__);
//    }];
//    [op3 addExecutionBlock:^{
//        NSLog(@"%@:%s start",[NSThread currentThread],__func__);
//        [NSThread sleepForTimeInterval:0.1];
//        NSLog(@"%@:%s end",[NSThread currentThread],__func__);
//    }];
//    Operation *op4=[[Operation alloc]init];
//    [queue addOperation:op1];
//    [queue addOperation:op2];
//    [queue addOperation:op3];
//    [queue addOperation:op4];
//}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSOperationQueue *queue=[[NSOperationQueue alloc]init];
    queue.maxConcurrentOperationCount=1;
    
    NSBlockOperation *op1=[NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"download1 %@:%s start",[NSThread currentThread],__func__);
        NSOperationQueue *mainqueue=[NSOperationQueue mainQueue];
        [mainqueue addOperationWithBlock:^{
            self.view.backgroundColor=[UIColor yellowColor];
        }];
    }];
    NSBlockOperation *op2=[NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"download2 %@:%s start",[NSThread currentThread],__func__);
        NSOperationQueue *mainqueue=[NSOperationQueue mainQueue];
        [mainqueue addOperationWithBlock:^{
            self.view.backgroundColor=[UIColor redColor];
        }];
    }];
    NSBlockOperation *op3=[NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"download3 %@:%s start",[NSThread currentThread],__func__);
        NSOperationQueue *mainqueue=[NSOperationQueue mainQueue];
        [mainqueue addOperationWithBlock:^{
            self.view.backgroundColor=[UIColor greenColor];
        }];
    }];
    NSBlockOperation *op4=[NSBlockOperation blockOperationWithBlock:^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"download4 %@:%s start",[NSThread currentThread],__func__);
        NSOperationQueue *mainqueue=[NSOperationQueue mainQueue];
        [mainqueue addOperationWithBlock:^{
            self.view.backgroundColor=[UIColor blueColor];
        }];
    }];
    [op4 addDependency:op1];
    [op1 addDependency:op2];
    [op2 addDependency:op3];
    
    
    
    [queue addOperation:op1];
    [queue addOperation:op2];
    [queue addOperation:op3];
    [queue addOperation:op4];
    
}
-(void)download1
{
    NSLog(@"%@:%s start",[NSThread currentThread],__func__);
    [NSThread sleepForTimeInterval:0.1];
    NSLog(@"%@:%s end",[NSThread currentThread],__func__);

}
-(void)download2
{
    NSLog(@"%@:%s start",[NSThread currentThread],__func__);
    [NSThread sleepForTimeInterval:0.1];
    NSLog(@"%@:%s end",[NSThread currentThread],__func__);
    
}
- (IBAction)pressed1:(id)sender
{
    self.queue=[[NSOperationQueue alloc]init];
    self.queue.maxConcurrentOperationCount=1;
    [self.queue addOperationWithBlock:^{
        for(int i=0;i<50;i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download1 %@",i,[NSThread currentThread]);
        }
    }];
    [self.queue addOperationWithBlock:^{
        for(int i=0;i<50;i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download2 %@",i,[NSThread currentThread]);
        }
    }];
    [self.queue addOperationWithBlock:^{
        for(int i=0;i<50;i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download3 %@",i,[NSThread currentThread]);
        }
    }];
    [self.queue addOperationWithBlock:^{
        for(int i=0;i<50;i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download4 %@",i,[NSThread currentThread]);
        }
    }];
    [self.queue addOperationWithBlock:^{
        for(int i=0;i<50;i++)
        {
            [NSThread sleepForTimeInterval:0.1];
            NSLog(@"%i download5 %@",i,[NSThread currentThread]);
        }
    }];
}
- (IBAction)pressed2:(UIButton *)sender
{
    self.queue.suspended=!self.queue.suspended;
    [sender setTitle:self.queue.suspended?@"继续":@"暂停" forState:UIControlStateNormal];
}
- (IBAction)pressed3:(id)sender
{
    self.queue=[[NSOperationQueue alloc]init];
    Operation *op=[[Operation alloc]init];
    [self.queue addOperation:op];
}
- (IBAction)pressed4:(id)sender
{
    [self.queue cancelAllOperations];
}
@end
